package com.example.k8s.hello.service.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.example.k8s.hello.bean.sys.entity.SysOssEntity;
import com.example.k8s.hello.bean.sys.query.SysOssQuery;
import com.example.k8s.hello.boot.oss.service.IOssService;
import com.example.k8s.hello.common.bean.Page;
import com.example.k8s.hello.common.exception.ApiExEnum;
import com.example.k8s.hello.service.sys.service.SysOssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * 文件上传
 *
 * @author Mark sunlightcs@gmail.com
 */
@Tag(name = "文件上传")
@RestController
@RequestMapping("sys/oss")
@RequiredArgsConstructor
public class SysOssController {
	private final SysOssService sysOssService;

    private final IOssService ossService;

	/**
	 * 文件分页
	 */
	@Operation(summary = "文件分页")
	@GetMapping("/list")
	@SaCheckPermission("sys:oss:all")
	public Page list(@ParameterObject SysOssQuery query, @ParameterObject Page page) {
		return sysOssService.queryPage(query, page);
	}

	/**
	 * 上传文件
	 */
	@Operation(summary = "上传文件")
	@PostMapping("/upload/{bucket}")
	public String upload(@RequestParam("file") MultipartFile file, @PathVariable String bucket) {
		ApiExEnum.A0010.assertTrue(!file.isEmpty(), "上传文件不能为空");

		String name = ossService.uploadFile(file, bucket);

		String url = new OssEntity(bucket, name).getUrl();

		//保存文件信息
		SysOssEntity ossEntity = new SysOssEntity();
		ossEntity.setUrl(url);
		ossEntity.setFileName(file.getOriginalFilename());
		sysOssService.save(ossEntity);

		return url;
	}

	/**
	 * 下载文件
	 */
	@Operation(summary = "下载文件")
	@GetMapping("/download/{url}")
	public void download(@PathVariable String url, HttpServletResponse response) {
		OssEntity ossEntity = new OssEntity(url);

		ossService.download(ossEntity.name, ossEntity.bucket, response);
	}

	/**
	 * 下载缩略图
	 */
	@Operation(summary = "下载缩略图")
	@GetMapping("/download-thumbnail/{url}/{targetWidth}/{targetHeight}")
	public void downloadThumbnail(@PathVariable String url, @PathVariable Integer targetWidth, @PathVariable Integer targetHeight
			, HttpServletResponse response){

		OssEntity ossEntity = new OssEntity(url);

		ossService.downloadThumbnail(ossEntity.name, ossEntity.bucket, targetWidth, targetHeight, response);
	}

	/**
	 * 下载水印图片
	 */
	@Operation(summary = "下载水印图片")
	@GetMapping("/download-watermark/{url}")
	public void downloadWatermark(@PathVariable String url, HttpServletResponse response){

		OssEntity ossEntity = new OssEntity(url);

		ossService.downloadWatermark(ossEntity.name, ossEntity.bucket, response);
	}

	/**
	 * 文件删除
	 */
	@Operation(summary = "文件删除")
	@PostMapping("/delete")
	@SaCheckPermission("sys:oss:all")
	public void delete(@RequestBody Long[] ids){
		sysOssService.removeByIds(Arrays.asList(ids));
	}

	public class OssEntity {
		String bucket;
		String name;

		private static final String SEPARATOR = ":";

		public OssEntity(String bucket, String name) {
			this.bucket = bucket;
			this.name = name;
		}

		public OssEntity(String url) {
			String[] strs = url.split(SEPARATOR);

			if (strs.length != 2) {
				ApiExEnum.A0010.thrown("文件路径不合法");
			}
			this.bucket = strs[0];
			this.name = strs[1];
		}

		private String getUrl() {
			return bucket + SEPARATOR + name;
		}
	}
}
